import { Component, OnInit } from '@angular/core';
import { News } from '../../model/news';
import { NewsService } from '../../service/news.service';
import { Router, ActivatedRoute } from '@angular/router';
import { ConfigService } from '../../service/config.service';

@Component({
  selector: 'app-managelist',
  templateUrl: './managelist.component.html',
  styleUrls: ['./managelist.component.css']
})
export class ManagelistComponent implements OnInit {
  nl:Array<News>;
  HOST:string;
  constructor(
    private newsservice:NewsService,
    private router:Router,
    private routerinfo:ActivatedRoute,
    private config:ConfigService
    ) {
    this.nl=new Array();
    this.HOST=config.host+"/public/"

   }

  ngOnInit() {
    this.loadnewslist();
  }
  

  private loadnewslist(){
    this.nl=new Array();
    this.newsservice.getNewsList()
    .then((data:any)=>{
      this.nl=data;
  
    })
  
  }


  addNews(){
    this.router.navigate(['editnews','add']);
  }
  updateNews(id:string){
    this.router.navigate(['editnews',id]);
  }

  editNews(id:string){

    this.router.navigate(['editnews',id]);

  }

  deleteNews(id:string){

    this.newsservice.deleteNews(id)
    .then((flag:boolean)=>{
      if(flag==true){
        this.loadnewslist();
      }
      else{
        alert("删除异常，请刷新后重试")
      }

    })
    
  }

  toDetail(id:string){
    this.router.navigate(['detail',id]);
  }








  selectedFile: string;
 
  onFileChanged(event) {
    this.selectedFile = event.target.files[0];
   
  }


  uploadNewsCover(newsid:string){


   this.newsservice.uploadNewsCoverPic(newsid,this.selectedFile)
   .then((data:any)=>{


   if(data.filename){
     this.loadnewslist()
   }


   })


  }



}
